#include "listecreatures.h"
#include <stdio.h>
#include <stdlib.h>

void initNbCreatures(ListeCreatures *pListeCreatures)
{
     pListeCreatures->nbCreatures=0;
}

int getNbCreatures(ListeCreatures *pListeCreatures)
{
    return(pListeCreatures->nbCreatures);
}

Creature* getCreature(ListeCreatures *pListeCreatures, int id)
{
    return(pListeCreatures->tabCreatures[id]);
}

void initTabCreatures(ListeCreatures *pListeCreatures)
{

     int i;

     for(i=0;i<=14;i++)
     {
                       pListeCreatures->tabCreatures[i]=0;
     }
}

void ajouterCreature(ListeCreatures *pListeCreatures, Creature *pCreature)
{
     int nbCreatures;
     nbCreatures=getNbCreatures(pListeCreatures);

     pListeCreatures->tabCreatures[nbCreatures]=pCreature;


     pListeCreatures->nbCreatures=pListeCreatures->nbCreatures+1;
}

void supprimerCreature(ListeCreatures *pListeCreatures, int id)
{
     int nbCreatures;
     Creature *creature;

     nbCreatures=getNbCreatures(pListeCreatures);


     if(id<nbCreatures-1)
     {
         for(id=id;id<nbCreatures-1;id++)
         {
             creature=getCreature(pListeCreatures, id+1);
             setCreaId(creature, id);
             pListeCreatures->tabCreatures[id]=creature;
         }

         creature=pListeCreatures->tabCreatures[nbCreatures-1];
         creature=NULL;

         free(creature);

         pListeCreatures->nbCreatures=pListeCreatures->nbCreatures-1;
      }

      else
      {
          pListeCreatures->tabCreatures[id]=NULL;
          pListeCreatures->nbCreatures=pListeCreatures->nbCreatures-1;
      }


}

ListeCreatures* nouvelleListeCreatures(int numeroVague)
{
     FILE* fichier;
     int i,id=0;
     int type1,type2,type3,type4,type5,temp;

     Creature *creature;
     ListeCreatures *listeCreatures;

     fichier=fopen("vagues.vag","r");

     listeCreatures = (ListeCreatures*) malloc (sizeof(ListeCreatures));


     initTabCreatures(listeCreatures);
     initNbCreatures(listeCreatures);

     printf("ok");

     for(i=0;i<(numeroVague-1)*5;i++)
        fscanf(fichier,"%d",&temp);

     fscanf(fichier,"%d",&type1);
     fscanf(fichier,"%d",&type2);
     fscanf(fichier,"%d",&type3);
     fscanf(fichier,"%d",&type4);
     fscanf(fichier,"%d",&type5);

     printf("***%d***",type1);
     printf("***%d***",type2);
     printf("***%d***",type3);
     printf("***%d***",type4);
     printf("***%d***",type5);

     for(i=0;i<type1;i++)
     {
         creature=creerNouvelleCreature(1,id);
         ajouterCreature(listeCreatures,creature);
         id++;
     }

     for(i=0;i<type2;i++)
     {
         creature=creerNouvelleCreature(2,id);
         ajouterCreature(listeCreatures,creature);
         id++;
     }

     for(i=0;i<type3;i++)
     {
         creature=creerNouvelleCreature(3,id);
         ajouterCreature(listeCreatures,creature);
         id++;
     }

     for(i=0;i<type4;i++)
     {
         creature=creerNouvelleCreature(4,id);
         ajouterCreature(listeCreatures,creature);
         id++;
     }

     for(i=0;i<type5;i++)
     {
         creature=creerNouvelleCreature(5,id);
         ajouterCreature(listeCreatures,creature);
         id++;
     }


     /*switch(numeroVague)
     {
                        case 1:
                             for(i=0;i<15;i++)
                             {
                                              creature=creerNouvelleCreature(1,i);
                                              ajouterCreature(listeCreatures,creature);
                             }
                             break;
                        case 2:
                             for(i=0;i<15;i++)
                             {
                                              creature=creerNouvelleCreature(2,i);
                                              ajouterCreature(listeCreatures,creature);
                             }
                             break;
                        case 3:
                             for(i=0;i<15;i++)
                             {
                                              creature=creerNouvelleCreature(3,i);
                                              ajouterCreature(listeCreatures,creature);
                             }
                             break;
                        case 4:
                             for(i=0;i<15;i++)
                             {
                                              creature=creerNouvelleCreature(4,i);
                                              ajouterCreature(listeCreatures,creature);
                             }
                             break;
                        case 5:
                             for(i=0;i<15;i++)
                             {
                                              creature=creerNouvelleCreature(5,i);
                                              ajouterCreature(listeCreatures,creature);
                             }
                             break;
     }
     */

     fclose(fichier);

     return(listeCreatures);


}


void libereListeCreatures(ListeCreatures *pListeCreatures)
{
    int i,nbCreatures;
    nbCreatures=getNbCreatures(pListeCreatures);

    for(i=0;i<nbCreatures;i++)
    {
       supprimerCreature(pListeCreatures,i);
    }

    free(pListeCreatures);
}



